import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker'
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker'
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
import * as monaco from 'monaco-editor'
import {jsonTem, vueTem,htmlTem,cssTem,jsTem} from './temDefault.js'

export function useInit(container, codeType) {
    self.MonacoEnvironment = {
        getWorker: function (workerId, label) {
            const getWorkerModule = (moduleUrl, label) => {
                return new Worker(self.MonacoEnvironment.getWorkerUrl(moduleUrl), {
                    name: label, type: 'module'
                })
            }

            switch (label) {
                case 'json':
                    return new jsonWorker()
                case 'css':
                case 'scss':
                case 'less':
                    return new cssWorker()
                case 'html':
                case 'handlebars':
                case 'razor':
                    return new htmlWorker()
                case 'typescript':
                case 'javascript':
                    return new tsWorker()
                default:
                    return new EditorWorker()
            }
        }
    }
    let initContent = jsonTem
    switch (codeType) {
        case 'json':
            initContent = jsonTem
            break
        case 'vue':
            initContent = vueTem
            break
        case 'html':
            initContent = htmlTem
            break
        case 'css':
            initContent = cssTem
            break
        case 'js':
            initContent = jsTem
            break
    }
    let monacoEditor = monaco.editor.create(container.value, {
        value: initContent,
        language: 'html',
        theme: 'vs-dark',
        wordWrap: 'on',
        minimap: {
            autohide: true,
            enabled: false
        }
    })

    monacoEditor.layout() //编辑区域自适应

    window.addEventListener('resize', () => {
        console.log('resize')
        monacoEditor.layout()
    })

    //自定义指令
    var fizzCommand = monacoEditor.createContextKey('test', true)
    monacoEditor.addCommand(
        monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Shift | monaco.KeyCode.KeyL), function () {
            // const currentModel = editor.getModel()
            //
            // const lineCount = currentModel.getLineCount()
            // const valueLength = currentModel.getValueLength()
            // const options = currentModel.getOptions()
            console.log('dddddddddddddddd')
        }, 'test')


    return monacoEditor
}
